package com.shengdacar.sharelibrary.qq;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;

import com.shengdacar.sharelibrary.callback.AsyncCallBack;
import com.shengdacar.sharelibrary.callback.NoInstalled;
import com.shengdacar.sharelibrary.utils.BitmapSaveAsyncTask;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

import java.io.File;

/**
 * 作者：huyi
 * 日期：2020/12/17 10:12
 * 描述：
 */
public class QqShareUtil {

    private QqShareUtil() {

    }

    public static QqShareUtil newInstance() {
        return new QqShareUtil();
    }

    private NoInstalled noInstalled;

    /**
     * 注册微信未安装监听
     */
    public QqShareUtil setNoInstalled(NoInstalled noInstalled) {
        this.noInstalled = noInstalled;
        return this;
    }

    private NoInstalled getNoInstalled() {
        if (null == noInstalled) {
            noInstalled = (transaction) -> {
            };
        }
        return noInstalled;
    }

    /**
     * qq文本分享
     *
     */
    public void qqTxtShare(Context context, String msg) {
        if (qqNoInstalled(context)) {
            return;
        }
        if (TextUtils.isEmpty(msg)) {
            getNoInstalled().noInstalled("分享的文本是空");
            return;
        }
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
        sendIntent.setType("text/plain");
        sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
        context.startActivity(Intent.createChooser(sendIntent, "选择分享途径"));
    }


    /**
     * qq bitmap图片分享
     *
     * @param activity 上下文
     * @param bitmap   分享的bitmap
     * @param path     bitmap存储的路径
     */
    public void qqBitmapShare(Activity activity, Bitmap bitmap, String path) {
        if (qqNoInstalled(activity)) {
            return;
        }
        if (bitmap == null) {
            getNoInstalled().noInstalled("分享的图片为空");
            return;
        }
        if (TextUtils.isEmpty(path)) {
            path = activity.getExternalFilesDir(null).getAbsolutePath();
        }
        String fileName = "img_" + System.currentTimeMillis() + ".jpg";
        BitmapSaveAsyncTask bitmapSaveAsyncTask = new BitmapSaveAsyncTask(path, fileName, file -> {
            bitmap.recycle();
            Bundle params = new Bundle();
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, file.getAbsolutePath());
            QqApiGlobal.getInstance().getmTencent().shareToQQ(activity, params, iUiListener);
        });
        bitmapSaveAsyncTask.execute(bitmap);
    }

    /**
     * qq 文件类型的图片分享
     *
     * @param activity 上下文
     * @param file     图片文件
     */
    public void qqFilePictrueShare(Activity activity, File file) {
        if (qqNoInstalled(activity)) {
            return;
        }
        if (file == null) {
            getNoInstalled().noInstalled("分享的图片为空");
            return;
        }
        Bundle params = new Bundle();
        //params.putString(QQShare.SHARE_TO_QQ_APP_NAME, appName);
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, file.getAbsolutePath());
        QqApiGlobal.getInstance().getmTencent().shareToQQ(activity, params, iUiListener);
    }


    /**
     * QQ好友分享图文消息
     *
     * @param activity  上下文
     * @param title     分享的标题
     * @param summary   分享的消息摘要
     * @param targetUrl 这条分享消息被好友点击后的跳转URL
     * @param resLogo   资源图片
     * @param path      资源图片存储的路径
     */
    public void qqHttpShare(Activity activity, String title, String summary, String targetUrl, int resLogo, String path) {
        if (qqNoInstalled(activity)) {
            return;
        }
        if (TextUtils.isEmpty(path)) {
            path = activity.getExternalFilesDir(null).getAbsolutePath();
        }
        String fileName = "img_" + System.currentTimeMillis() + ".jpg";
        Bitmap bmp = BitmapFactory.decodeResource(activity.getResources(), resLogo);
        BitmapSaveAsyncTask bitmapSaveAsyncTask = new BitmapSaveAsyncTask(path, fileName, new AsyncCallBack() {
            @Override
            public void fileBack(File file) {
                bmp.recycle();
                Bundle params = new Bundle();
                params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
                //分享的标题。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_	SUMMARY不能全为空，最少必须有一个是有值的。
                params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
                //分享的消息摘要，最长50个字
                params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
                //这条分享消息被好友点击后的跳转URL。
                params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, targetUrl);
                //图片链接
//                params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl);
                //本地图片
                params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, file.getAbsolutePath());
                QqApiGlobal.getInstance().getmTencent().shareToQQ(activity, params, iUiListener);
            }
        });
        bitmapSaveAsyncTask.execute(bmp);
    }


    /**
     * 注册回调接口，并检查是否安装qq
     */
    private boolean qqNoInstalled(Context context) {
        //检查是否安装微信客户端
        if (!QqApiGlobal.getInstance().getmTencent().isQQInstalled(context)) {
            getNoInstalled().noInstalled("请安装QQ客户端");
            return true;
        }
        return false;
    }

    private IUiListener iUiListener = new IUiListener() {
        @Override
        public void onComplete(Object o) {
            getNoInstalled().noInstalled("分享完成");
        }

        @Override
        public void onError(UiError uiError) {
            getNoInstalled().noInstalled("分享失败");
        }

        @Override
        public void onCancel() {
            getNoInstalled().noInstalled("分享取消");
        }

        @Override
        public void onWarning(int i) {

        }
    };
}
